/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ventas.beans;

import entities.Pedido;
import java.io.IOException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import javax.enterprise.event.Observes;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import qualifiers.EventoEntidadElegida;
import ventas.excel.ExcelPedido;

/**
 *
 * @author damfercer
 */
@Named(value = "excelPedidoBean")
@SessionScoped
public class ExcelPedidoBean implements Serializable {
    private Pedido pedido;
    @Inject
    ExcelPedido excelPedidoMaker;
    /**
     * Creates a new instance of ExcelPedidoBean
     */
    public ExcelPedidoBean() {
        excelPedidoMaker=new ExcelPedido();
    }
    
    public String hacerExcel() throws IOException{
        FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        externalContext.setResponseContentType("application/vnd.ms-excel");
        externalContext.setResponseHeader("Content-Disposition", "attachment; filename=\"Pedido.xls\"");
        HSSFWorkbook workbook=excelPedidoMaker.hacerExcel(pedido);
        workbook.write(externalContext.getResponseOutputStream());
        facesContext.responseComplete();
        return null;
    }

    public void onExcelPedido(@Observes @EventoEntidadElegida Pedido p){
        this.pedido=p;
    }
    /**
     * @return the pedido
     */
    public Pedido getPedido() {
        return pedido;
    }

    /**
     * @param pedido the pedido to set
     */
    public void setPedido(Pedido pedido) {
        this.pedido = pedido;
    }
}
